package com.tuocent.dagv2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tuocent.dagv2.common.AjaxResult;
import com.tuocent.dagv2.common.entity.LoginUserInfo;
import com.tuocent.dagv2.common.utils.TCUtil;
import com.tuocent.dagv2.common.utils.redis.RedisCache;
import com.tuocent.dagv2.constant.PublicConst;
import com.tuocent.dagv2.entity.LoginLogs;
import com.tuocent.dagv2.mapper.LoginLogsMapper;
import com.tuocent.dagv2.service.ILoginLogsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import eu.bitwalker.useragentutils.Version;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.time.LocalDateTime;
import java.util.Enumeration;
import java.util.Map;

/**
 * <p>
 * 用户登录LOG 服务实现类
 * </p>
 *
 * @author Vincent
 * @since 2025-07-22
 */
@Service
public class LoginLogsServiceImpl extends ServiceImpl<LoginLogsMapper, LoginLogs> implements ILoginLogsService {
    @Resource
    private RedisCache redis;

    @Override
    public void loginLog(HttpServletRequest request, String account, String info, String access) {
        UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        Enumeration<String> h = request.getHeaderNames();

        Browser browser = userAgent.getBrowser();
        OperatingSystem operatingSystem = userAgent.getOperatingSystem();
        Version browserVersion = userAgent.getBrowserVersion();
        LoginLogs loginLogs = new LoginLogs();
        loginLogs.setIp(TCUtil.getClientIp(request));
        loginLogs.setOperatingSystem(operatingSystem.getName());
        loginLogs.setBrowser(browser.getName());
        if (browserVersion != null) {
            loginLogs.setBrowserVersion(browserVersion.getVersion());
        }
        loginLogs.setAccess(access);
        loginLogs.setUserCode(account);
        loginLogs.setCreateTime(LocalDateTime.now());
        loginLogs.setEvent(info);
        this.save(loginLogs);
    }

    @Override
    public String loginLogInfo(Model model) {
        return "/logs/loginLog";
    }

    @Override
    public AjaxResult getLoginLogData(HttpSession session, Map<String, Object> params) {
        LoginUserInfo loginUser = redis.get(session.getId());
        if (loginUser == null) {
            return AjaxResult.error("没有找到登录用户信息，请重新登录");
        }
        int page = PublicConst.DEF_PAGE;
        int limit = PublicConst.DEF_LIMIT;
        QueryWrapper<LoginLogs> query = new QueryWrapper<LoginLogs>()
                .orderByAsc("create_time");
        for (Object obj : params.keySet()) {
            String key = obj.toString();
            String keyValue = params.get(key).toString();
            if (keyValue.trim().isEmpty()) {
                continue;
            }
            switch (key) {
                case "page":
                    page = Integer.parseInt(keyValue);
                    break;
                case "limit":
                    limit = Integer.parseInt(keyValue);
                    break;
                default:
                    query.eq(key, keyValue);
                    break;
            }
        }
        Page<LoginLogs> data = page(new Page<>(page, limit), query);
        return AjaxResult.success(data.getRecords(), data.getTotal());
    }
}
